﻿using DryIoc;
using MaterialDesignThemes.Wpf;
using Mododor.Ext;
using Mododor.JsonModels;
using Mododor.Models;
using Mododor.ViewInterfaces;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Mododor.ViewModels.CompanyInfoViewModels
{
    internal class CompanyInfoViewModel : ViewInterface<CompanyInfoModel>, INavigationAware
    {
        private CompanyInfoModel companyInfoModel;

        public CompanyInfoModel CompanyInfoModel
        {
            get { return companyInfoModel; }
            set { companyInfoModel = value; RaisePropertyChanged(); }
        }

        public CompanyInfoViewModel(HttpHelper httpHelper, IDialogService dialogService) : base(dialogService, httpHelper)
        {
            //DataStatsValueList = new ObservableCollection<CompanyInfoModel>();

            //SelectionChangedDataGrid = new DelegateCommand<CompanyInfoModel>((x) =>
            //{
            //    if (x is null)
            //    {
            //        return;
            //    }
            //    //快速反射赋值
            //    CompanyInfoModel = QuickRefValueForMore<CompanyInfoModel>.SetValueForMore(CompanyInfoModel, x);
            //});

            MarkingDeleteCommand = new DelegateCommand(() =>
            {
                DialogParameters dialogPara = new DialogParameters()
                {
                    {"Key","确认是否删除" }
                };

                _dialogService.ShowDialog("AskingDialogView", dialogPara, async (res) =>
                {
                    if (res.Result == ButtonResult.OK)
                    {
                        string resJson = await httpHelper.GetJson($"{ExtUrl.CompanyMarkingDeleteUrl}?idCompany={CompanyInfoModel.IdCompany}");
                        await GetAllConpanyInfo();
                    }
                }, "DialogWindowView");
            });

            SaveDataCommand = new DelegateCommand(async () =>
            {
                string json = JsonConvert.SerializeObject(CompanyInfoModel);
                string res = await httpHelper.PostJson(ExtUrl.CompanyOperationUrl, json);
                JustHaveStausAndMsgInfoJsonModel.Rootobject root = JsonConvert.DeserializeObject<JustHaveStausAndMsgInfoJsonModel.Rootobject>(res);
                SnackBarMessage = root.msg;
                await SnackBarWaiting();
                //更新界面数据
                await GetAllConpanyInfo();
            });
            ClearDataCommand = new DelegateCommand(() =>
            {
                CompanyInfoModel = new CompanyInfoModel();
            });
        }

        public async Task GetAllConpanyInfo()
        {
            try
            {
                IsOpen = true;
                CompanyInfoModel = new CompanyInfoModel();
                DataStatsValueList.Clear();
                string res = await _httpHelper.GetJson(ExtUrl.CompanySearchAllUrl);
                CompanyInfoJsonSearchModel.Rootobject root = JsonConvert.DeserializeObject<CompanyInfoJsonSearchModel.Rootobject>(res);
                foreach (CompanyInfoJsonSearchModel.Res msg in root.res)
                {
                    DataStatsValueList.Add(new CompanyInfoModel()
                    {
                        CompanyAdd = msg.companyAdd,
                        CompanyCity = msg.companyCity,
                        CompanyNameCh = msg.companyNameCh,
                        CompanyNameEn = msg.companyNameEn,
                        CompanyTel = msg.companyTel,
                        CompanyZip = msg.companyZip,
                        Id = msg.id,
                        IdCompany = msg.idCompany,
                    });
                }
                IsOpen = false;


                SnackBarMessage = root.msg;
            }
            catch (Exception ex)
            {
                SnackBarMessage = ex.Message;
            }
            finally
            {
                await SnackBarWaiting();
            }

        }

        protected override async Task SnackBarWaiting()
        {
            await base.SnackBarWaiting();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            await GetAllConpanyInfo();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
